import Vue from "vue";
import Router from "vue-router";
import Home from "./views/Home.vue";
import utils from "@/utils/utils";
import request from "@/utils/request";
import global_ from "./global.config";
import wxsdk from "@/utils/wxsdk";

// import mfActRouter from "@/views/mfAct/router";
// import ztActRouter from "@/views/ztAct/router";
// import hcActRouter from "@/views/hcAct/router";
// import jbActRouter from "@/views/jbAct/router";
// import keppelRouter from "@/views/keppel/router";
// import bgyActRouter from "@/views/bgyAct/router";
// import keppelRouter from "@/views/keppel/router";
// import zdfzhRouter from "@/views/zdfzh/router";//中鼎房展会
// import aiAssistantRouter from "@/views/aiAssistant/router";  //ai小助理\
// import gzldRouter from "@/views/gzldAct/router"; //赣州绿地\
// import tjkeppelRouter from "@/views/tjkeppel/router";//天津吉宝
// import jxbgyRouter from "@/views/jxbgy/router"; //江西碧桂园
import manycustomersRouter from "@/views/manycustomers/router";//很多客


Vue.use(Router);
const router = new Router({
  mode: "history",
  base: process.env.BASE_URL,
  routes: [
    {
      path: "/",
      name: "home",
      meta: {
        requiresAuth: 1 //2 无需授权 1用户授权 0或者不传表示静默授权
      },
      component: Home
    },
    // {
    //   //吉宝主页
    //   path: "/homepage",
    //   name: "homepage",
    //   meta: {
    //     keepAlive: true,
    //     title: "keppel",
    //     requiresAuth: 2 //2 无需授权 1用户授权 0或者不传表示静默授权
    //   },
    //   // route level code-splitting
    //   // this generates a separate chunk (about.[hash].js) for this route
    //   // which is lazy-loaded when the route is visited.
    //   component: () => import("./views/jb-pages/HomePage.vue")
    // },
    {
      path: "/redirect",
      name: "redirect",
      meta: {
        requiresAuth: 2 //2 无需授权 1用户授权 0或者不传表示静默授权
      },
      component: () => import("./views/Redirect.vue")
    },
    {
      path: "*",
      name: "comingsoon",
      meta: {
        title: "敬请期待",
        requiresAuth: 2 //2 无需授权 1用户授权 0或者不传表示静默授权
      },
      component: () => import("./views/ComingSoon.vue")
    },
    // ...mfActRouter,
    // ...ztActRouter,
    // ...hcActRouter
    // ...jbActRouter
    // ...keppelRouter
    // ...bgyActRouter,
    // ...keppelRouter
    // ...zdfzhRouter
    // ...aiAssistantRouter //ai助理
    // ...gzldRouter //赣州绿地
    // ...tjkeppelRouter //天津吉宝
    // ...jxbgyRouter  //江西碧桂园
    ...manycustomersRouter //很多客


  ]
});

// 全局路由守卫
router.beforeEach((to, from, next) => {
  // to: Route: 即将要进入的目标 路由对象
  // from: Route: 当前导航正要离开的路由
  // next: Function: 一定要调用该方法来 resolve 这个钩子。执行效果依赖 next 方法的调用参数。

  //初始化Cookies(openid,passport...)
  utils.initCookies(to.query);

  //设置页面title
  if (to.meta.title) {
    document.title = to.meta.title;
  }
  //判断授权方式
  if (!to.query.j) {
    request
      .get(global_.API_URL, {
        action: "getConfig"
      })
      .then(response => {
        global_.OAUTH_URL = response.obj.auth_url;
        global_.APPID = response.obj.appid;
        global_.SHARE_API = response.obj.share_api;
        //微信sdk初始化
        global_.SHARE_CALLBACK_OK = function() {};
        if (to.meta.requiresAuth && to.meta.requiresAuth === 2) {
          next();
        } else if (to.query.from && to.query.from === "h5") {
          next();
        } else {
          utils.needsAuth(to.meta.requiresAuth, () => {
            wxsdk.init(() => next());
          });
        }
      });
  }

  // router.push({ name: 'home' });
  // next({ path: '/error', replace: true, query: { noGoBack: true } })
});

export default router;
